<?php

/**
 * manage base action
 * login / logout and something
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class SiteController extends CController {
    public function actionLogin() {
        $model = new LoginForm;
        
        //check post

        if (r()->isPostRequest) {
            
            //exit();
            $model->attributes = $_POST['LoginForm'];
            
            //validate and login
            if ($model->validate() && $model->login()) {
                
                User::model()->saveLastLogin(user()->getUserProperty('user_id'));
                //redirect to backend
                $this->redirect(array('dashboard/index'));
                app()->end();
                
            } else {
                //print_r($model->getErrors());
                
            }
        }
        
        $this->layout = 'login';
        $this->render('login', compact('model'));
    }

    /**
     * logout
     */
    public function actionLogout() {
        Yii::app()->user->logout();
        //redirect to home page
        $this->redirect(array('login'));
    }

}